package com.jy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.good.result.GetGoodsIndexSwiperResult;
import com.jy.entity.Cart;
import com.jy.entity.GoodsIndexSwiper;
import com.jy.mapper.CartMapper;
import com.jy.mapper.GoodsIndexSwiperMapper;
import com.jy.service.GoodsIndexSwiperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品首页轮播图表 服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-11-14
 */
@Service
public class GoodsIndexSwiperServiceImpl extends ServiceImpl<GoodsIndexSwiperMapper, GoodsIndexSwiper> implements GoodsIndexSwiperService {

    private final GoodsIndexSwiperMapper goodsIndexSwiperMapper;

    private final CartMapper cartMapper;

    public GoodsIndexSwiperServiceImpl(GoodsIndexSwiperMapper goodsIndexSwiperMapper, CartMapper cartMapper) {
        this.goodsIndexSwiperMapper = goodsIndexSwiperMapper;
        this.cartMapper = cartMapper;
    }

    @Override
    public ResponseWrapper getGoodsIndexPageSwiperApi(Integer currentLoginUserAid) {

        // 返回结果
        HashMap<String, Object> result = new HashMap<>();

        // 获取当前登录用户的购物车商品数量
        Cart cart = cartMapper.selectOne(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUserAid, currentLoginUserAid)
        );
        if (cart == null) {
            // 购物车为空
            result.put("cartGoodsNum",0);
        }else {
            if(cart.getIsEmpty()){
                result.put("cartGoodsNum",0);
            }else {
                result.put("cartGoodsNum",cart.getGoodsNum());
            }
        }

        // 获取轮播图5张图
        List<GetGoodsIndexSwiperResult> goodsIndexPageSwipers = goodsIndexSwiperMapper.getGoodsIndexPageSwiper();
        result.put("goodsIndexPageSwipers",goodsIndexPageSwipers);

        return ResponseWrapper.markSuccess(result);
    }
}
